package service.controller;

import foundation.annotation.Auth;
import foundation.annotation.LoginUser;
import model.entity.MyClass;
import model.entity.User;
import model.sys.BLogicException;
import model.sys.ErrorMessage;
import model.sys.RestResultList;
import model.sys.SingleValueResult;
import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import javax.ws.rs.*;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

/**
 * @author futao
 * Created on 18-7-19-上午4:52.
 */
@Controller
@Path("Hello")
@Produces(MediaType.APPLICATION_JSON)
public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);


    @GET
    @Path("say")
    @LoginUser
    public SingleValueResult say() {
        return new SingleValueResult("你好");
    }

    @GET
    @Path("/")
    @LoginUser
    public User say2(
            @QueryParam("username") String username
            , @CookieParam("lastVisitTime") Cookie cookie
    ) throws UnsupportedEncodingException {
        System.out.println("controller-cookie:" + URLDecoder.decode(cookie.getValue(), String.valueOf(Charsets.UTF_8)));
        MyClass aClass = new MyClass("微创班", 123, "江西省南昌市");
        User user = new User("付韬", 18, aClass, "上海市闵行区");
        return user;
    }

    @GET
    @Path("login")
    @Auth
    public SingleValueResult login() {
        return new SingleValueResult("login");
    }


    @Path("exception")
    @GET
    public SingleValueResult exception() {
        throw new BLogicException(ErrorMessage.SYS_EXCEPTION);
    }

    /**
     * 添加用户
     *
     * @param name
     * @param age
     * @param userAddress
     * @param classNO
     * @param className
     * @param classAddress
     * @return
     */
    @POST
    @Path("add")
    public User add(
            @FormParam("name") String name,
            @FormParam("age") int age,
            @FormParam("userAddress") String userAddress,
            @FormParam("classNO") int classNO,
            @FormParam("className") String className,
            @FormParam("classAddress") String classAddress
    ) {
        return new User(name, age, new MyClass(className, classNO, classAddress), userAddress);
    }

    @GET
    @Path("list")
    public RestResultList<User> list() {
        RestResultList<User> restResultList = new RestResultList<User>();
        ArrayList<User> users = new ArrayList<User>();
        User user = new User("futao", 12, new MyClass("不知道", 12, "上海市"), "南京西路");
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);
        restResultList.setList(users);
        logger.info("略略略");
        logger.debug("正在debug");
        logger.error("不可能发生错误");
        logger.warn("警告");
        return restResultList;
    }
}
